layui.use(['layer','form','table','element','layedit','upload','laydate','transfer'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,laydate = layui.laydate
        ,transfer = layui.transfer
        ,upload = layui.upload;

    var E = window.wangEditor
    var editor = new E('#addEdit')
    // 或者 var editor = new E( document.getElementById('editor') )
    editor.customConfig.uploadImgServer = '/admin/project/uploadProjectImg2'
    editor.customConfig.uploadFileName = 'file'
    // 自定义菜单配置
    editor.customConfig.menus = [
        'head',  // 标题
        'fontSize',  // 字号
        'foreColor',  // 文字颜色
        'backColor',  // 背景颜色
        'link',  // 插入链接
        'justify',  // 对齐方式
        'quote',  // 引用
        'image',  // 插入图片
        'undo',  // 撤销
        'redo',  // 重复，
        'qsy'  // 去水印
    ]
    editor.create();

    var advertContentList;
    var gameAdvertContentList;
    var taskAdvertContentList;
    var preContentList;
    var endContentList;
    var allPart;

    //动态加载关联app
    $.ajax({
        type:"GET",
        url:"/admin/project/part/query?page=1&limit=1000",
        async:false,
        success:function (result) {
            advertContentList = [];
            gameAdvertContentList = [];
            taskAdvertContentList = [];
            preContentList = [];
            endContentList = [];
            allPart = result.data;
            result.data.forEach(function (r){
                if(r.type == "PRE_CONTENT"){
                    preContentList.push(r)
                }else if(r.type == "END_CONTENT"){
                    endContentList.push(r)
                }else if(r.type == 'GAME_CONTENT'){
                    gameAdvertContentList.push(r);
                }else if(r.type == 'TASK_CONTENT') {
                    taskAdvertContentList.push(r);
                }
            })

        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

    //显示搜索框
    transfer.render({
        elem: '#taskAdvertContent'
        ,width:'400px'
        ,data: taskAdvertContentList
        ,title: ['软文广告', '软文广告']  //自定义标题
        ,id:'transferTaskAdvertData'
        ,showSearch: true
        ,parseData:function (res){
            return {
                "value":res.id,
                "title":res.content
            }
        }
    });

    //显示搜索框
    transfer.render({
        elem: '#gameAdvertContent'
        ,width:'400px'
        ,data: gameAdvertContentList
        ,title: ['软文广告', '软文广告']  //自定义标题
        ,id:'transferGameAdvertData'
        ,showSearch: true
        ,parseData:function (res){
            return {
                "value":res.id,
                "title":res.content
            }
        }
    });

    //显示搜索框
    transfer.render({
        elem: '#preContent'
        ,data: preContentList
        ,width:'400px'
        ,title: ['前文', '前文']  //自定义标题
        ,id:'transferPreData'
        ,showSearch: true
        ,parseData:function (res){
            return {
                "value":res.id,
                "title":res.content
            }
        }
    });

    //显示搜索框
    transfer.render({
        elem: '#endContent'
        ,data: endContentList
        ,width:'400px'
        ,title: ['文末', '文末']  //自定义标题
        ,id:'transferEndData'
        ,showSearch: true
        ,parseData:function (res){
            return {
                "value":res.id,
                "title":res.content
            }
        }
    });
    
    $("#create").on("click",function () {
        var taskAdvertData = transfer.getData('transferTaskAdvertData');
        var gameAdvertData = transfer.getData('transferGameAdvertData');
        var preData = transfer.getData('transferPreData');
        var endData = transfer.getData('transferEndData');
        var content = "";
        if(preData != null && preData.length >0){
            preData.forEach(function (obj) {
                content += obj.title;
            });
        }
        if(taskAdvertData != null && taskAdvertData.length >0){
            taskAdvertData.forEach(function (obj) {
                content += obj.title;
            });
        }
        if(gameAdvertData != null && gameAdvertData.length >0){
            gameAdvertData.forEach(function (obj) {
                content += obj.title;
            });
        }
        if(endData != null && endData.length >0){
            endData.forEach(function (obj) {
                content += obj.title;
            });
        }

        editor.txt.html(content);
    })

    //提交表单
    form.on('submit(form)',function (data) {

        //校验类型和标签是否选择
        layer.msg('保存中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        var taskAdvertData = transfer.getData('transferTaskAdvertData');
        var gameAdvertData = transfer.getData('transferGameAdvertData');
        var advertIdData = [];
        if(taskAdvertData != null && taskAdvertData.length >0){
            taskAdvertData.forEach(function (obj) {
                advertIdData.push(obj.value);
            });
        }
        if(gameAdvertData != null && gameAdvertData.length >0){
            gameAdvertData.forEach(function (obj) {
                advertIdData.push(obj.value);
            });
        }

        var preData = transfer.getData('transferPreData');
        var preIdData = [];
        if(preData != null && preData.length >0){
            preData.forEach(function (obj) {
                preIdData.push(obj.value);
            });
        }

        var endData = transfer.getData('transferEndData');
        var endIdData = [];
        if(endData != null && endData.length >0){
            endData.forEach(function (obj) {
                endIdData.push(obj.value);
            });
        }

        var paramData = {
            preIds:preIdData,
            endIds:endIdData,
            advertIds:advertIdData,
            title:$("#title").val(),
            keyword:$("#keyword").val(),
            spiderId:$("#spiderId").val(),
            content:editor.txt.html(),
            isTemp:$('input[name="isTemp"]:checked ').val(),
        };
        var data2 = JSON.stringify(paramData);
        //保存项目
        $.ajax({
            url:"/admin/project/part/createProject",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                if(result.flag){
                    layer.msg(result.message);
                    //清空内容
                    // $("#form").reset();
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });

});

